package so.cuo.platform.chartboost
{
	import flash.events.EventDispatcher;
	import flash.events.StatusEvent;
	import flash.external.ExtensionContext;
	import flash.system.Capabilities;

	[Event(name="onInterstitialDismiss", type="so.cuo.platform.chartboost.ChartboostEvent")]
	[Event(name="onInterstitialFailedReceive", type="so.cuo.platform.chartboost.ChartboostEvent")]
	[Event(name="onInterstitialLeaveApplication", type="so.cuo.platform.chartboost.ChartboostEvent")]
	[Event(name="onInterstitialPresent", type="so.cuo.platform.chartboost.ChartboostEvent")]
	[Event(name="onInterstitialReceive", type="so.cuo.platform.chartboost.ChartboostEvent")]
	public class Chartboost extends EventDispatcher
	{
//		protected var isTesting:String="";
		protected var bannerKeys:Array;
		protected var moreAppKeys:Array;
		protected var interstitialKeys:Array;
		protected var extensionContext:ExtensionContext=null;
		private var hasInterstitialInited:Boolean=false;
		private static var instance:Chartboost;

		public static function getInstance():Chartboost
		{
			if (instance == null)
			{
				instance=new Chartboost();
			}
			return instance;
		}

		//aneName "so.cuo.ane.Admob"
		public function Chartboost()
		{
			var aneName:String="so.cuo.platform.chartboost";
			extensionContext=ExtensionContext.createExtensionContext(aneName, null);
			if (extensionContext != null)
			{
				extensionContext.addEventListener(StatusEvent.STATUS, onAdHandler);
			}
		}

		public function dispose():void
		{
			if (extensionContext != null)
			{
				extensionContext.removeEventListener(StatusEvent.STATUS, onAdHandler);
				extensionContext.dispose();
				extensionContext=null;
			}
			instance=null;
		}

		protected function onAdHandler(e:StatusEvent):void
		{
			trace("ane event type:" + e.type, "code:" + e.code, "level:" + e.level);
			dispatchEvent(new ChartboostEvent(e.code, e.level));
		}

		public function get supportDevice():Boolean
		{
			var os:String=Capabilities.os;
			os=os.toLowerCase();
			var osInfo:Array=os.split("os ");
			var num:Number=parseFloat(osInfo[1]);
			if (Capabilities.manufacturer.indexOf("iOS") > -1 && num < 6)
			{ //ios 6一下设备不支持
				trace("chartboost crash on ios low than ios 6,so return false");
				return false;
			}
			var ok:Boolean=Capabilities.manufacturer.indexOf('iOS') > -1 || Capabilities.manufacturer.indexOf('Android') > -1;
			return ok && this.extensionContext != null;
		}

		public function get supportedMoreApp():Boolean
		{
			return supportDevice;
		}

		public function get supportedInterstitial():Boolean
		{
			return supportDevice;
		}

		//------------------------
/*		public function setTesting(isTesting:String=null):void
		{
			this.isTesting=isTesting;
		}*/

		public function setInterstitialKeys(appID:String, key:String=null):void
		{
			this.interstitialKeys=[appID, key];
			this.fillEmptyKeys(appID, key);
		}

		public function setMoreAppKeys(appID:String, key:String=null):void
		{
			this.moreAppKeys=[appID, key];
			fillEmptyKeys(appID, key);
		}

		private function fillEmptyKeys(appID:String, key:String):void
		{
			if (this.bannerKeys == null)
			{
				this.bannerKeys=[appID, key];
			}
			if (this.moreAppKeys == null)
			{
				this.moreAppKeys=[appID, key];
			}
			if (this.interstitialKeys == null)
			{
				this.interstitialKeys=[appID, key]
			}
		}

//----------------------------
		public function showInterstitial(location:String="default"):void
		{
			if (this.supportedInterstitial)
			{
				extensionContext.call(ChartboostFunNames.showInterstitial, location);
			}
		}

		public function isInterstitialReady(location:String="default"):Boolean
		{
			if (this.supportedInterstitial && this.hasInterstitialInited)
			{
				return extensionContext.call(ChartboostFunNames.isInterstitialReady, location);
			}
			return false;
		}

		public function cacheInterstitial(location:String="default"):void
		{
			if (this.supportedInterstitial)
			{
				if (!this.hasInterstitialInited)
				{
					extensionContext.call(ChartboostFunNames.initInterstitial, this.interstitialKeys[0], this.interstitialKeys[1]);
					this.hasInterstitialInited=true;
				}
				extensionContext.call(ChartboostFunNames.cacheInterstitial, location);
			}
		}

		public function showMoreApp():void
		{
			if (this.supportedMoreApp)
			{
				extensionContext.call(ChartboostFunNames.showMoreApp);
			}
		}

		public function isMoreAppReady():Boolean
		{
			if (this.hasInterstitialInited && this.supportedMoreApp)
			{
				return extensionContext.call(ChartboostFunNames.isMoreAppReady);
			}
			return false;
		}

		public function cacheMoreApp():void
		{
			if (this.supportedMoreApp)
			{
				if (!this.hasInterstitialInited)
				{
					extensionContext.call(ChartboostFunNames.initInterstitial, this.interstitialKeys[0], this.interstitialKeys[1]);
					this.hasInterstitialInited=true;
				}
				extensionContext.call(ChartboostFunNames.cacheMoreApp);
			}
		}
	}
}
